package abs.controller;

import abs.factory.ResultFactory;
import abs.service.TranslateService;
import abs.service.UserService;
import abs.utils.FileUtil;
import abs.vo.FileHistory;
import abs.vo.Result;
import abs.vo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.websocket.server.PathParam;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class TranslateCrl {
    @Autowired
    private TranslateService translateService;
    @Autowired
    private UserService userService;

    /**
     * 把原文摘要，返回结构
     * @param rankMethod 摘要算法
     * @param origin 原文
     * @return 摘要结果
     */
    @RequestMapping(value = "/api/handleOrigin", method = RequestMethod.POST, produces = "application/json; charset = UTF-8")
    @ResponseBody
    public Result handleOrigin(@PathParam("rankMethod")String rankMethod, @PathParam("origin")String origin){
        if ("textRank".equals(rankMethod)) {
            Map<String, List<String>> result = translateService.textRank(origin);
            if (null == result) {
                return ResultFactory.buildFailResult("处理过程发生异常");
            }
            return ResultFactory.buildSuccessResult(result);
        }
        if ("mmr".equals(rankMethod)) {
            List<String> result = translateService.mmr(origin);
            if (null == result) {
                return ResultFactory.buildFailResult("处理过程发生异常");
            }
            return ResultFactory.buildSuccessResult(result);
        }
        if ("word2vec".equals(rankMethod)) {
            List<String> result = translateService.word2vec(origin);
            if (null == result) {
                return ResultFactory.buildFailResult("处理过程发生异常");
            }
            return ResultFactory.buildSuccessResult(result);
        }
        return ResultFactory.buildSuccessResult("success");
    }

    /**
     * 获取用户的 历史文件上传记录
     * @param userName 用户名
     * @return 历史记录List
     */
    @RequestMapping(value = "/api/getHistory", method = RequestMethod.POST, produces = "application/json; charset = UTF-8")
    @ResponseBody
    public Result getHistory(@PathParam("userName")String userName){
        List<FileHistory> resultList = translateService.getHistoryByUserName(userName);
        return ResultFactory.buildSuccessResult(resultList);
    }

    /**
     * 用户点击历史记录后从 阿里云获取历史记录的文件 并处理
     * @param userName 用户名
     * @param filePath 阿里云服务器上的文件路径
     * @param fileType 文件类型
     * @param fileName 文件名
     * @return 历史记录读取结果
     */
    @RequestMapping(value = "/api/handleHistory", method = RequestMethod.POST, produces = "application/json; charset = UTF-8")
    @ResponseBody
    public Result handleHistory(@PathParam("userName")String userName, @PathParam("filePath")String filePath, @PathParam("fileType")String fileType, @PathParam("fileName")String fileName){
        Map<String, Object> result = translateService.getFileAndAbstract(userName, filePath, fileType, fileName);
        return ResultFactory.buildSuccessResult(result);
    }

    /**
     * 读取文件内容
     * @param fileType 文件类型
     * @param file 文件本体
     * @param userName 用户名
     * @return 读取结果
     */
    @RequestMapping(value = "/api/fileToWords", method = RequestMethod.POST, produces = "application/json; charset = UTF-8")
    @ResponseBody
    public Result fileToWords(@RequestParam("fileType")String fileType, @RequestParam("file") MultipartFile file, @RequestParam("userName") String userName){
        Map<String, Object> resultMap = new HashMap<>();
        String result = "";
        String fileName = file.getOriginalFilename();
        File f = FileUtil.transferToFile(file);
        if (!"null".equals(userName)) {
            System.out.println("to save file on oss");
            //把文件存为历史记录
            FileHistory fileHistory = translateService.saveFileOnOss(f, userName, fileType, fileName);
            resultMap.put("fileHistory", fileHistory);
        }
        if ("audio".equals(fileType)) {
            result = translateService.audioToWords(f);
            if (result.contains("onebest")) {
                resultMap.put("result", result);
                return ResultFactory.buildSuccessResult(resultMap);
            } else {
                return ResultFactory.buildFailResult(result);
            }
        }
        if ("pic".equals(fileType)) {
            result = translateService.picToWords(f);
            System.out.println(result);
            if (null != result) {
                resultMap.put("result", result);
                return ResultFactory.buildSuccessResult(resultMap);
            }
        }
        return ResultFactory.buildFailResult("出现错误");
    }

    /**
     * 更改用户名
     * @param userInfo 用户信息对象
     * @return 修改后的用户信息对象信息对象
     */
    @RequestMapping(value = "/api/live/changeUserInfo", method = RequestMethod.POST, produces = "application/json; charset = UTF-8")
    @ResponseBody
    public Result changeUserInfo(@RequestBody UserInfo userInfo){
        userService.changeUserInfo(userInfo);
        UserInfo userInfo1 = userService.findUserByName(userInfo.getUserName());
        return ResultFactory.buildSuccessResult(userInfo1);
    }

    /**
     * 修改用户密码
     * @param userName 用户名
     * @param oldPassword 旧密码
     * @param newPassword 新密码
     * @return 修改结果
     */
    @RequestMapping(value = "/api/live/changePassword", method = RequestMethod.GET, produces = "application/json; charset = UTF-8")
    @ResponseBody
    public Result changePassword(@PathParam("userName")String userName, @PathParam("oldPassword")String oldPassword, @PathParam("newPassword")String newPassword){
        UserInfo user = userService.login(userName, oldPassword);
        if (null == user){
            return ResultFactory.buildFailResult("旧密码错误");
        }else{
            userService.changePassword(userName, newPassword);
            return ResultFactory.buildSuccessResult("密码修改成功");
        }
    }
}
